/**
 * Created by 张伊 on 2017/10/31.
 */
mainApp.factory("dayclearFactory",function(){
	
	var service={};

    //获取全部列表
    service.getListData=function(currentindex){
        var obj={
            "pageIndex": currentindex,
            "pageSize": 8
        }
        var list = "";
        RequestUtils.postJson('../sumday/getNow',obj).done(function (resp) {
            if(resp.resultCode==0){
                console.log(resp.data);
                list=resp.data;
                CommonUtils.buildFailHint("列表获取","成功");
            }else{
                CommonUtils.buildFailHint('列表获取', '失败');
            }
        });

        return list;
    };

    //新增日清数据的service
	service.addDayclear=function(dayclearpObj){
	    console.log(dayclearpObj);
		RequestUtils.postForm('../sumday/add',dayclearpObj).done(function (resp) {
            if(resp.resultCode==0){
                CommonUtils.buildFailHint('记录新增', '成功');
                location.reload(true);
            }else{
                CommonUtils.buildFailHint('记录新增', '失败');
            }
		});;
	}

	//修改日清数据的service
	service.UpdateDayclear=function(dayclearObj){
	    console.log("更新的数据");
		console.log(dayclearObj[0]);
	    RequestUtils.postForm('../sumday/update', dayclearObj)
        .done(function (resp) {
            if(resp.resultCode==0){
                CommonUtils.buildFailHint('记录修改', '成功');
                location.reload(true);
            }else{
                CommonUtils.buildFailHint('记录修改', '失败');
            }
        });
	}

	//根据工号(id)查询日清记录service
	service.selectDayclearById=function(id){
        console.log("进入selectDayclearById");
        var list="";
		RequestUtils.getJson("../sumday/getByIdInweek/"+id).done(function(resp){

            if(resp.resultCode==0){
            	list=resp.data;
                console.log("list");
                console.log(list);
                CommonUtils.buildFailHint('记录查询', '成功');
                //location.reload(true);
            }else{
                CommonUtils.buildFailHint('记录查询', '失败');
            }
		});
		console.log("返回list");
		return list;
	}

	//删除日清记录service
	service.delete=function(id){
        RequestUtils.getJson("../sumday/delete/"+id).done(function(resp){
            if(resp.resultCode==0){
                CommonUtils.buildFailHint('记录删除', '成功');
                location.reload(true);
            }else{
                CommonUtils.buildFailHint('记录删除', '失败');
            }
        })
	};

	//按日期区间和工号查询日清记录service
    service.selectDayclearBynumAndDate=function(dayclObj){
        var obj={
            "pageIndex": 1,
            "pageSize": 8
        }

        var list="";
        RequestUtils.postJson("../sumday/getByJobnumTime/"+dayclObj.jobnum+"/"+dayclObj.startDate+"/"+dayclObj.endDate,obj)
            .done(function(resp){
            if(resp.resultCode==0){
                console.log(resp);
                list=resp.data.items;
                CommonUtils.buildFailHint('记录查询', '成功');
                //location.reload(true);
            }else{
                CommonUtils.buildFailHint('记录查询', '失败');
            }
        })

        console.log(list);
        return list;
    };

	    return service;
})

mainApp.controller("dayclearController",function($scope,$http,dayclearFactory){
	$scope.title="日清管理";
    console.log($scope.title);
	
	//获取数据 全部的
	$scope.dayclearList=dayclearFactory.getListData(1);
    //获取list
    $scope.list=$scope.dayclearList.items;
    console.log($scope.list);



    //绑定数据到模态框
	$scope.addDayclearData;

	//打开新增记录模态框
	$scope.openAddaddDayclearModal=function(){
		$("#addDayclearModal").modal("show");
	};

    //新增日清记录
	$scope.addDayclear=function(dayclear){

		var dayclearObj={

			"date":dayclear.date,
            "task":dayclear.task,
            "result":dayclear.result,
            "incomplete_rate":dayclear.incomplete_rate,
            "remark":dayclear.remark,
            "employee.id":1
		};
        console.log(dayclearObj);
		dayclearFactory.addDayclear(dayclearObj);

		//关闭模态框
		$("#addDayclearModal").modal("hide");
		$scope.$apply();//自动刷新
	};

	//日清结果下拉框
    $scope.results=[
        {name:"处理中",value:"0"},
        {name:"已完成",value:"1"}
    ];


	$scope.dayclear;
	//修改模态框，据工号查询
	$scope.selectDayclear=function(id){
	    console.log(id);
		var daycl=dayclearFactory.selectDayclearById(id);
        console.log("dayclear的值");
        $scope.dayclears=daycl;
        console.log($scope.dayclears);

        $("#dayclearModal").modal("show");

        //日期的计算
        var date=CommonUtils.calculateDate($scope.dayclears.date);
        $scope.startDay=date[0];
        $scope.endDay=date[1];
        $scope.weekDay=date[2];
    };

	//修改后的提交
	$scope.Update=function(dayclearObjs){
	    console.log(dayclearObjs);
        dayclearFactory.UpdateDayclear(dayclearObjs);

        $("#dayclearModal").modal("hide");
        $scope.$apply();
	};



	//删除日清记录
	$scope.delete=function(id){
		dayclearFactory.delete(id);
	};



	//据时间区间和工号条件查询
    $scope.dayclearWhere;
    $scope.selectWhere=function(dayclearWhereObj){
        console.log(dayclearWhereObj);
        var dayclearData=dayclearFactory.selectDayclearBynumAndDate(dayclearWhereObj);
        $scope.list=dayclearData;
        console.log(dayclearData);
    }


    //打开统计模态框
    $scope.openDayclearDataModal=function(id){
        var DayclearData=dayclearFactory.selectDayclearById(id);
        console.log(DayclearData);
        //填充列表数字
        var myChart = echarts.init(document.getElementById('dayclearChart'));
        var obj2={
            data:[

            ]
        };
        for (var i = 0; i < DayclearData.length; i++) {
            var data={value:DayclearData[i].counts,name:dayclearConst[DayclearData[i].result]};
            obj2.data.push(data);
        }

        var option=CommonUtils.getAttendOption(obj2,dayclearStatusData,'日清记录');
        // 使用刚指定的配置项和数据显示图表。
        myChart.setOption(option);

        $("#DayclearDataModal").modal("show");

    }

    //翻页
    $scope.NextListData=function(pageIndex){
		$scope.dayclearList=dayclearFactory.getListData(pageIndex);
        $scope.list=$scope.dayclearList.items;

    }

    CommonUtils.setDatetimepicker("startDatetimepicker");
    CommonUtils.setDatetimepicker("endDatetimepicker");
    CommonUtils.setDatetimepicker("selectDatetimepicker");
    CommonUtils.setDatetimepicker("datetimepicker");
	CommonUtils.setDatetimepicker("updateDatetimepicker");
})